/*
 * $Id: AddItemBase.java 26 2013-05-20 06:00:59Z antonyhands@gmail.com $
 */
package ru.olympus.antonyhands.ao.ui;

import ru.olympus.antonyhands.ao.UIHelper;
import ru.olympus.antonyhands.ao.data.AOData;
import ru.olympus.antonyhands.ao.data.entities.CraftSkill;
import ru.olympus.antonyhands.ao.data.entities.ItemCategory;
import ru.olympus.antonyhands.ao.data.util.EItemParentType;
import ru.olympus.antonyhands.ao.ui.listener.CancelButtonClickListener;
import ru.olympus.antonyhands.ao.util.StringUtilities;

import com.vaadin.annotations.AutoGenerated;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Button.ClickEvent;

/**
 * Компонент добавления категории предмета или крафтового навыка
 * 
 * @author AntonyHands
 */
public class AddItemBase extends CustomComponent
{
    private static final long serialVersionUID = -2433304150255745727L;

    private EItemParentType _type;

    @AutoGenerated
    private AbsoluteLayout mainLayout;
    @AutoGenerated
    private Button buttonCancel;
    @AutoGenerated
    private Button buttonOK;
    @AutoGenerated
    private TextField textFieldTitle;
    @AutoGenerated
    private TextField textFieldCode;
    @AutoGenerated
    private Label labelTitle;
    @AutoGenerated
    private Label labelCode;

    /*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

    /*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

    /*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

    /*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

    /*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

    /*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

    /**
     * The constructor should first build the main layout, set the
     * composition root and then do any custom initialization.
     *
     * The constructor will not be automatically regenerated by the
     * visual editor.
     */
    public AddItemBase(EItemParentType type)
    {
        _type = type;
        buildMainLayout();
        setCompositionRoot(mainLayout);

        // TODO add user code here
    }

    @AutoGenerated
    private AbsoluteLayout buildMainLayout()
    {
        // common part: create layout
        mainLayout = new AbsoluteLayout();
        mainLayout.setImmediate(false);
        mainLayout.setWidth("360px");
        mainLayout.setHeight("240px");
        mainLayout.setMargin(false);

        // top-level component properties
        setWidth("360px");
        setHeight("240px");

        // labelCode
        labelCode = new Label();
        labelCode.setImmediate(false);
        labelCode.setWidth("-1px");
        labelCode.setHeight("-1px");
        labelCode.setValue("Код");
        mainLayout.addComponent(labelCode, "top:40.0px;left:40.0px;");

        // labelTitle
        labelTitle = new Label();
        labelTitle.setImmediate(false);
        labelTitle.setWidth("-1px");
        labelTitle.setHeight("-1px");
        labelTitle.setValue("Наименование");
        mainLayout.addComponent(labelTitle, "top:100.0px;left:40.0px;");

        // textFieldCode
        textFieldCode = new TextField();
        textFieldCode.setImmediate(false);
        textFieldCode.setWidth("-1px");
        textFieldCode.setHeight("-1px");
        mainLayout.addComponent(textFieldCode, "top:40.0px;left:163.0px;");

        // textFieldTitle
        textFieldTitle = new TextField();
        textFieldTitle.setImmediate(false);
        textFieldTitle.setWidth("-1px");
        textFieldTitle.setHeight("-1px");
        mainLayout.addComponent(textFieldTitle, "top:100.0px;left:163.0px;");

        // buttonOK
        buttonOK = new Button();
        buttonOK.setCaption("OK");
        buttonOK.setImmediate(false);
        buttonOK.setWidth("-1px");
        buttonOK.setHeight("-1px");
        buttonOK.addListener(new ClickListener()
        {
            private static final long serialVersionUID = 4147559031729036113L;

            @Override
            public void buttonClick(ClickEvent event)
            {
                if (StringUtilities.isEmpty((String)textFieldCode.getValue())
                        || StringUtilities.isEmpty((String)textFieldTitle.getValue()))
                    return;
                String code = (String)textFieldCode.getValue();
                textFieldCode.setValue("");
                String title = (String)textFieldTitle.getValue();
                textFieldTitle.setValue("");

                if (EItemParentType.ITEM_CATEGORY.equals(_type))
                    AOData.getInstance().addItemCategory(new ItemCategory(code, title));
                else if (EItemParentType.CRAFT_SKILL.equals(_type))
                    AOData.getInstance().addCraftSkill(new CraftSkill(code, title));
                else
                    return;
                UIHelper.clearScreen();
                UIHelper.addStatusLabel("Действие успешно выполнено");
            }
        });
        mainLayout.addComponent(buttonOK, "top:174.0px;left:61.0px;");

        // buttonCancel
        buttonCancel = new Button();
        buttonCancel.setCaption("Отмена");
        buttonCancel.setImmediate(false);
        buttonCancel.setWidth("-1px");
        buttonCancel.setHeight("-1px");
        buttonCancel.addListener(new CancelButtonClickListener());
        mainLayout.addComponent(buttonCancel, "top:174.0px;left:180.0px;");

        return mainLayout;
    }

}
